package com.credit.jumu.playcard.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.ColorUtils;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.playcard.BuildConfig;
import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.base.BaseActivity;
import com.credit.jumu.playcard.cardapp.CardApp;
import com.credit.jumu.playcard.fragment.BillTabFragment;
import com.credit.jumu.playcard.fragment.HomeTabFragment;
import com.credit.jumu.playcard.fragment.MySetTabFragment;
import com.credit.jumu.playcard.fragment.PlanTabFragment;
import com.credit.jumu.playcard.utils.DataSharedPreference;
import com.credit.jumu.playcard.utils.JptAdapter;
import com.credit.jumu.playcard.utils.LogUtils;
import com.credit.jumu.playcard.utils.MD5Encoder;
import com.credit.jumu.playcard.utils.MPermissionUtils;
import com.credit.jumu.playcard.utils.SignUtil;
import com.credit.jumu.playcard.utils.StatusBarCompat;
import com.credit.jumu.playcard.utils.SystemUtil;
import com.credit.jumu.playcard.utils.ToastUtil;
import com.credit.jumu.playcard.utils.UrlsUtils;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

public class MainActivity extends AppCompatActivity implements OnTabSelectListener {
	private List<Fragment> list = new ArrayList<>();
	private BillTabFragment billTabFragment;
	private HomeTabFragment homeTabFragment;
	private MySetTabFragment mySetTabFragment;
	private PlanTabFragment planTabFragment;

	private JPTabBar mTabbar;
	private ViewPager mPager;
	private TextView title, settexicon;
	private ImageView imageIcon;
	private RelativeLayout apptitle;
	@Titles
	private static final String[] mTitles = {"首页", "还款计划", "我的账单", "我的"};
	@SeleIcons
	private static final int[] mSeleIcons = {R.mipmap.click_home, R.mipmap.click_refund, R.mipmap.click_bil, R.mipmap.click_me};

	@NorIcons
	private static final int[] mNormalIcons = {R.mipmap.unclicked_home, R.mipmap.unclicked_refund, R.mipmap.unclicked_bill, R.mipmap.unclicked_me};
	private static final String[] tabTitle = {"首页", "还款计划", "账单", "我的"};
	private DataSharedPreference ds;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		StatusBarCompat.compat(this, Color.parseColor("#026ffe"));

		setContentView(R.layout.activity_main);
		mTabbar = (JPTabBar) findViewById(R.id.tabbar);
		mPager = (ViewPager) findViewById(R.id.view_pager);
		mTabbar.setSelectedColor(Color.parseColor("#2e7fd0"));
		title = (TextView) findViewById(R.id.title);
		title.setTextColor(this.getResources().getColor(R.color.white));
		imageIcon = (ImageView) findViewById(R.id.imageIcon);
		apptitle = (RelativeLayout) findViewById(R.id.apptitle);
		settexicon = (TextView) findViewById(R.id.settexicon);
		apptitle.setBackgroundColor(Color.parseColor("#026ffe"));
		imageIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, MessageActivity.class);
				startActivity(intent);
			}
		});

		MPermissionUtils.requestPermissionsResult(this, 10, new String[]{
				Manifest.permission.READ_PHONE_STATE,
				Manifest.permission.READ_EXTERNAL_STORAGE
		}, new MPermissionUtils.OnPermissionListener() {
			@Override
			public void onPermissionGranted() {
				showSystemParameter();
			}

			@Override
			public void onPermissionDenied() {
				MPermissionUtils.showTipsDialog(MainActivity.this, R.string.permissions_read_phone_state, true);
			}
		});
		initView();
		initData();
	}

	private void showSystemParameter() {
		if (null == ds) {
			ds = new DataSharedPreference(this);
		}
		ds.setUUID(SystemUtil.getAndroidId(getApplicationContext()));
	}

	private void initData() {
		//uv
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("source", CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CardApp.intefaceType);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);
		OkGo.<String>post(UrlsUtils.UV)//
				.tag(this)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								LogUtils.println("zhangyiyionSuccessUV------" + response.body());
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionErrorUV------" + response.body());
							}
						}


				);
	}


	protected void initView() {
		billTabFragment = new BillTabFragment();
		homeTabFragment = new HomeTabFragment();
		mySetTabFragment = new MySetTabFragment();
		planTabFragment = new PlanTabFragment();
		list.add(homeTabFragment);
		list.add(planTabFragment);
		list.add(billTabFragment);
		list.add(mySetTabFragment);

		mPager.setAdapter(new JptAdapter(getSupportFragmentManager(), list));
		mPager.setOffscreenPageLimit(4);
		mTabbar.setContainer(mPager);
		mTabbar.setTabListener(this);
	}


	//点击的页面
	@Override
	public void onTabSelect(int index) {
		title.setText(tabTitle[index].toString());

		if (3 == index) {
			imageIcon.setVisibility(View.GONE);
			settexicon.setVisibility(View.VISIBLE);
			settexicon.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(MainActivity.this, SetActivity.class);
					startActivity(intent);
				}
			});
		} else if (0 == index) {
			settexicon.setVisibility(View.GONE);
			imageIcon.setVisibility(View.VISIBLE);
			imageIcon.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(MainActivity.this, MessageActivity.class);
					startActivity(intent);
				}
			});
		} else {
			imageIcon.setVisibility(View.GONE);
			settexicon.setVisibility(View.GONE);
		}
//		ToastUtil.showShortToast("点击了" + index);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		ToastUtil.showShortToast("点击退出");
		this.finish();
	}
}
